package org.i1510.base.utils.io;

import java.io.IOException;
import java.io.InputStream;

/**
 * 加密流
 * 
 * @author 叶中奇
 * 
 */
public class EncryptionInputStream extends AbstractInputStream {
	public EncryptionInputStream(InputStream originStream) throws IOException {
		super(originStream);
	}

	public EncryptionInputStream(InputStream originStream, int encryptionMode)
			throws IOException {
		super(originStream, encryptionMode);
	}

	private boolean noEncryption;

	@Override
	protected void init() throws IOException {
		noEncryption = (cryptionMode != 1);
		if (noEncryption) {
			return;
		}
		additionalBytes = markBytes;
	}

	@Override
	protected void format(byte[] b, int off, int len) {
		if (noEncryption) {
			return;
		}
		for (int i = off; i < len; i++) {
			if (b[i] == 127) {
				b[i] = -128;
			} else {
				b[i]++;
			}
		}
	}

	@Override
	protected int format(int i) {
		if (noEncryption) {
			return i;
		}
		if (i == -1)
			return -1;
		if (i == 255)
			return 0;
		return i + 1;
	}

}
